boolean isMouseDown = false;
tShapeContainer globalShapeContainer = null;
int screenSize[] = {1294,800};
tClueOverlay globalClueOverlay = null;
boolean clueIsActive = false;
tPalette globalPalette = null;
int frameOnWhichMouseWasLastPressed = -1;
int globalGridSize = 1;
PFont menuFont = null;
// the level the user is on, for each difficulty, set in levelSetup()
int userLevel[] = {0,0,0};
tButtonContainer globalButtonContainer = null;

images img;
menu mainMenu;
PImage levelOutline;

SolarSystem userSystem;
SolarSystem compSystem;

//0 : into; 1 : menu; 2 : game
int gameState = 0;
//0 : easy; 1 : medium; 2 : hard
int difficulty = 0;

void setup()
{
  img = new images();
  mainMenu = new menu();
  levelOutline = loadImage("levels/temp.png");
  
  size(screenSize[0],screenSize[1],P3D);
  background(0);
  globalShapeContainer = new tShapeContainer();
  globalShapeContainer.initStandardShapes();
  globalClueOverlay = new tClueOverlay();
  globalPalette = new tPalette();
  menuFont = loadFont("data/SakkalMajallaBold-30.vlw");
  buttonSetup();
  levelSetup();
  scoreSetup();
  elementsSetup();
  clueSetup();
}

void draw()
{
  update();
  background(0);
  
  switch(gameState)
  {
    case 0:
      img.drawIntro();
      break;
    case 1:
      mainMenu.drawMenu();
      break;
    case 2:
      drawBackground();
      drawGame();
      break;
  }
}
void drawBackground()
{ 
  switch(difficulty)
  {
  case 0:
    image(img.easybg,0,0);
    break;
  case 1:
    image(img.medbg,0,0);
    break;
  case 2:
    image(img.hardbg,0,0);
    break;
  }
  image(levelOutline,162,100);

  //fill(0);
  //rect(162,100,970,600);
}

void drawGame()
{
  globalPalette.doDraw();
  globalShapeContainer.doDraw();
  globalButtonContainer.doDraw();
  globalClueOverlay.doDraw();
//  println(globalShapeContainer.getElementIDs(false));
}
void update()
{
  int[] mousePos = getMousePos();
  globalPalette.update(mousePos);
  globalShapeContainer.update(mousePos);
  globalButtonContainer.update(mousePos);
  globalClueOverlay.update(mousePos);
}

int[] getMousePos()
{
  int mousePos[] = {
    (int)mouseX, (int)mouseY
  };
  return mousePos;
}

void mousePressed()
{
  isMouseDown = true;
  frameOnWhichMouseWasLastPressed = frameCount;
}

void mouseReleased()
{
  isMouseDown = false;
}

boolean getMouseDownLeft()
{
  if (mouseButton == LEFT && isMouseDown && !clueIsActive)
  {
    return true;
  }
  else
  {
    return false;
  }
}

void keyPressed()
{
  if(key == 'g')
  {
   fill(0);
   rect(162,100,970,600);
   globalShapeContainer.doDraw();
   PImage sandbox = ripSandbox();
   PImage outline = convertToBW(sandbox);
   
   outline.save("levels/temp.png");
  } 
  if(key == 's')
  {
    fill(0);
    rect(162,100,970,600);
    globalShapeContainer.doDraw();
    PImage solvedSandbox = ripSandbox();
    PImage completedOutline = convertToBW(solvedSandbox);
    completedOutline.save("levels/userSol.png");
    PImage solution = loadImage("levels/temp.png");
   
    
    if(sameImage(completedOutline,solution))
     print("CORRECT SOLUTION!");
    else
     print("KEEP TRYING!");
   }
}
